import axios from "axios"
import nprogress from "nprogress"
import 'nprogress/nprogress.css' 
import detail from "@/store/Detail"
import users from "@/store/Users"

const requests = axios.create({
    baseURL: "/api",
    timeout: 5000,
})

// 请求拦截器
requests.interceptors.request.use((config) => {
    // console.log(store)
    if(detail.uuid_token) {
        // 请求头添加一个字段（userTempId）：和后台沟通好
        config.headers.userTempId = detail.uuid_token
    }
    if(users.token) {
        // 请求头添加一个字段（userTempId）：和后台沟通好
        config.headers.token = users.token
    }
    nprogress.start()
    return config
})

requests.interceptors.response.use((res) => {
    nprogress.done()
    return res.data
    }, err =>{
        alert("false")
        console.log(err)
    // return Promise.reject(new error('faile'))
})

export default requests
